package com.atguigu.edu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.edu.entity.Course;
import com.atguigu.edu.entity.Teacher;
import com.atguigu.edu.entity.subject.CoursePublishVo;
import com.atguigu.edu.entity.vo.CourseInfoVo;
import com.atguigu.edu.entity.vo.CourseQuery;
import com.atguigu.edu.entity.vo.TeacherQuery;
import com.atguigu.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-10
 */
@RestController
@RequestMapping("/edu/course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    //新增课程
    @PostMapping(value = "/addCourse")
    public R addCourse(@RequestBody CourseInfoVo courseInfoVo){
        String  courseId = courseService.saveCourse(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }

    //根据课程id查询课程信息
    @GetMapping(value = "/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
      CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
      return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //根据课程id修改课程信息
    @PostMapping(value = "/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }
    //根据课程id查询课程确认信息
    @GetMapping(value = "/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //最终发布课程
    @PostMapping(value = "/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        Course course = new Course();
        course.setId(id);
        //状态改为发布
        course.setStatus("Normal");
        boolean b = courseService.updateById(course);
        if(b) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "课程多条件分页查询")
    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@ApiParam(name = "current",value = "当前页",required = true) @PathVariable long current,
                                  @ApiParam(name = "limit",value = "每页个数",required = true) @PathVariable long limit,
                                  @RequestBody(required = false) CourseQuery courseQuery){
        Page<Course> pageCourse = new Page<Course>(current,limit);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<Course>();
        if (!StringUtils.isEmpty(courseQuery.getTitle())) {
            queryWrapper.like("title",courseQuery.getTitle());
        }
        if (!StringUtils.isEmpty(courseQuery.getStatus())) {
            queryWrapper.eq("status",courseQuery.getStatus());
        }
        if (!StringUtils.isEmpty(courseQuery.getBegin())) {
            queryWrapper.ge("gmt_create",courseQuery.getBegin());
        }
        if (!StringUtils.isEmpty(courseQuery.getEnd())) {
            queryWrapper.le("gmt_create",courseQuery.getEnd());
        }
        queryWrapper.orderByDesc("gmt_modified");
        courseService.page(pageCourse,queryWrapper);
        long total = pageCourse.getTotal();
        List<Course> records = pageCourse.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.deleteCourse(courseId);
        return  R.ok();
    }
}

